package foundation;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.GregorianCalendar;
import java.util.Scanner;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class FFileCommunication {
	
	public void createFile(String fileName) {
		try {
			File file = new File(fileName);
			file.createNewFile();
		} catch (IOException e) {}
	}
	
	public synchronized Document readXmlFile(String fileName) throws 
						ParserConfigurationException,SAXException,IOException {
		Document doc = null;
		File file = new File(fileName);
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db;
		db = dbf.newDocumentBuilder();
		doc = db.parse(file);
		doc.getDocumentElement().normalize();
		
		return doc;
	}
	
	public synchronized String getLine(String fileName) throws FileNotFoundException {
		String line = null;

		File file = new File(fileName);
		Scanner in;
		in = new Scanner(file);

		if (in.hasNext()) {
			line = in.nextLine();
		}

		in.close();

		return line;
	}
	
	public synchronized void removeLine(String fileName) 
									throws FileNotFoundException, IOException {
		File file = new File(fileName);
		File tempFile = new File("temp" + fileName);
		tempFile.createNewFile();
		FileWriter out = new FileWriter(tempFile);
		PrintWriter output = new PrintWriter(out);

		Scanner input;
		input = new Scanner(file);

		// Skip first line:
		if (input.hasNext()) {
			input.nextLine();
		}

		while (input.hasNext()) {
			output.println(input.nextLine());
		}

		input.close();
		output.close();

		//Remove old file:
		file.delete();
		tempFile.renameTo(file);
	}
	
	public synchronized void addLine(String fileName, String line) 
									throws FileNotFoundException, IOException {
		File file = new File(fileName);
		File tempFile = new File("temp" + fileName);
		tempFile.createNewFile();
		FileWriter out = new FileWriter(tempFile);
		PrintWriter output = new PrintWriter(out);

		Scanner input;
		input = new Scanner(file);

		while (input.hasNext()) {
			output.println(input.nextLine());
		}

		output.println(line);

		input.close();
		output.close();

		//Remove old file:
		file.delete();
		tempFile.renameTo(file);
	}

	public synchronized void addLineWithTime(String fileName, String line) 
									throws FileNotFoundException, IOException{
		File file = new File(fileName);
		File tempFile = new File("temp.txt");
		tempFile.createNewFile();
		FileWriter out = new FileWriter(tempFile);
		PrintWriter output = new PrintWriter(out);

		GregorianCalendar now = new GregorianCalendar();
		int month = now.get(GregorianCalendar.MONTH) + 1;
		String time = now.get(GregorianCalendar.YEAR) + "-" + 
		month + "-" + 
		now.get(GregorianCalendar.DAY_OF_MONTH) + " " + 
		now.get(GregorianCalendar.HOUR_OF_DAY) + ":" + 
		now.get(GregorianCalendar.MINUTE) + ":" + 
		now.get(GregorianCalendar.SECOND);

		line = time + ":\t" + line;

		Scanner input;
		input = new Scanner(file);

		while (input.hasNext()) {
			output.println(input.nextLine());
		}

		output.println(line);

		input.close();
		output.close();

		//Remove old file:
		file.delete();
		tempFile.renameTo(file);
	}
	
	public synchronized boolean isEmpty(String fileName) throws FileNotFoundException {
		File file = new File(fileName);
		Scanner input;
		input = new Scanner(file);
		boolean isEmpty = !input.hasNext();
		input.close();
		return isEmpty;
	}
}
